package com.cnb.telepathytalk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;

public class PopupActivity extends Activity implements OnClickListener {
	public static final String EXTRA_KEY_MESSAGE = "PopupActivity";

	public static boolean isShown;

	private String[] info;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_popup);

		getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
						| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();

		isShown = true;

		info = getIntent().getStringExtra(EXTRA_KEY_MESSAGE).split("!@");
		String message = "";
		int i = 2;
		while (true) {
			message += info[i++];
			if (info.length == i) {
				break;
			}
			message += "!@";
		}

		((TextView) findViewById(R.id.name)).setText(info[0]);
		((TextView) findViewById(R.id.text)).setText(message);

		((TextView) findViewById(R.id.show)).setOnClickListener(this);
		((TextView) findViewById(R.id.cancel)).setOnClickListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		isShown = false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.show:
			Intent intent = new Intent(PopupActivity.this, ChatingRoomActivity.class);
			intent.putExtra(ChatingRoomActivity.TAG, info[1]);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			break;

		case R.id.cancel:
			finish();
			break;
		}
	}
}